<?php
/**
 * Created by PhpStorm.
 * User: 程序猴
 * QQ: 505413
 * Date: 2018/3/30
 * Time: 21:46
 */

namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\template\driver;
class Base extends Controller
{
    public function _initialize()
    {
        if(!session('uid')||!session('username')){
            $this->error('请先登录系统','admin/login/index');
        }
        $auth = new \com\Auth();
        $module     = strtolower(request()->module());
        $controller = strtolower(request()->controller());
        $action     = strtolower(request()->action());
        $url        = "/".$module."/".$controller."/".$action;
        $notCheck = array(
            '/admin/index/index',
            '/admin/base/setmenu'
        );
        if(session('uid')!=1){
            if(!in_array($url,$notCheck)){
                if(!$auth->check($url,session('uid'))){
                    $this->error('抱歉，您没有权限');
                    return false;
                }
            }
        }
    }
    public function setmenu(){
        $data =  Db::name("menu")->order('sort asc')->field('id,pid,title,font,icon,url,spread')->where(array('status'=>1))->select();
        $data = $this->menuTree($data);
        $tree['data'] = array_values($data);
        $json = json_encode($tree);
        exit($json);
    }
    protected function menuTree($data , $pid = 0){
        $array = array();
        foreach ($data as $val){
            if($pid == $val['pid']){
                $children = $this->menuTree($data, $val['id']);
                !empty($children) && $val['children'] = $children;
                $array[] = $val;
            }
        }
        return $array;

    }
}